<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class Setup3Form extends MiniyunViewForm
{
    public  $userName;    public  $password;    public  $passwordConfirm;    public  $email;    public function init(){
        $this->userName="admin";
    }
    public function rules()
    {
        $valid = array(
        array('userName, password, passwordConfirm, email', 'required'),
        array('password', 'length', 'min'=>5, 'max'=>20),
        array('password', 'checkPassword'),
        array('passwordConfirm', 'length', 'min'=>5, 'max'=>20),
        array('passwordConfirm', 'compare', 'compareAttribute' => 'password'),
        array('email', 'email'),
        );
        return $valid;
    }
    public function attributeLabels()
    {
        return array(
		    'userName'=>'管理员名',
            'password'=>'新密码', 
		    'passwordConfirm'=>'确认新密码', 
		    'email'=>'电子邮件', 
        );
    }

    
    public function checkPassword(){
        if(!$this->hasErrors())
        {
            $strArr = explode(" ", $this->password);
            if(count($strArr)!=1){
                $this->addError('password','密码中不能有空格');
                return;
            }
        }
    }

    public function save(){
        if($this->validate()){
                        $salt = CUtils::genRandomString(6);
            $user = User::model()->findByAttributes(array("user_name" => $this->userName));
            if (!$user) {
                $user = new User();
                $user->user_uuid = uniqid();
                $user->user_name = $this->userName;
            }
            $user->salt      = $salt;
            $user->user_pass = CSecurity::signPasswd($this->password, $salt);
            $user->user_status=1;
            $user->save();
            $userMeta = UserMeta::model()->findByAttributes(array("meta_key" => "email",
			                                                     "user_id" => $user->id));
            if (!$userMeta) {
                $userMeta = new UserMeta();
                $userMeta->user_id = $user->id;
            }
            $userMeta->meta_key="email";
            $userMeta->meta_value=$this->email;
             
            $userMeta->save();
            return true;
        }
        return false;
    }
}